package com.framework.network.remote

import androidx.lifecycle.Lifecycle
import com.framework.network.manager.NetworkManager
import com.framework.network.utils.NetworkBreakException
import com.google.gson.JsonSyntaxException
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable
import org.koin.core.KoinComponent
import java.net.SocketTimeoutException

/**
 * @ClassName: NetworkObserver
 * @Description: 收到数据后，对数据得处理
 * @Author: you
 */
open class NetworkObserver<T>(var lifecycle: Lifecycle? = null) : SingleObserver<T>, KoinComponent {
    var startRequestMethod: (d: Disposable) -> Unit = {}
    var successMethod: (value: T?) -> Unit = {}
    var errorMethod: (e: Throwable) -> Unit = {}
    var endRequestMethod: () -> Unit = {}
    var isUnifiedHand: Boolean? = true

    override fun onSubscribe(d: Disposable) {
        startRequestMethod(d)
    }

    override fun onSuccess(value: T & Any) {
        if (Lifecycle.State.DESTROYED == lifecycle?.currentState) {
            return
        }
        successMethod(value)
        endRequestMethod()
    }

    override fun onError(e: Throwable) {
        if (Lifecycle.State.DESTROYED == lifecycle?.currentState) {
            return
        }
        //没有数据，但是是成功状态，适用于返回数据结构里的data为空的情况
        if (e is NoDataApiException) {
            successMethod(null)
            endRequestMethod()
            return
        }
        if (e is LoginInvalidApiException) {
            NetworkManager.getInstance().create().mOnNetworkResultListener?.onThrowableError(e)
            errorMethod(e)
            endRequestMethod()
            return
        }
        if (isUnifiedHand == true) {
            when (e) {
                is NetworkBreakException -> {//断网
                    NetworkManager.getInstance().create().mOnNetworkResultListener?.onNetworkBreak(e)
                }
                is SocketTimeoutException -> {//超时错误
                    NetworkManager.getInstance().create().mOnNetworkResultListener?.onSocketTimeout(e)
                }
                is JsonSyntaxException -> { //数据解析错误
                    NetworkManager.getInstance().create().mOnNetworkResultListener?.onJsonSyntax(e)
                }
                is ErrorApiException -> {//服务端返回错误码
                    NetworkManager.getInstance().create().mOnNetworkResultListener?.onServiceCallBackError(e)
                }
                else -> {
                    NetworkManager.getInstance().create().mOnNetworkResultListener?.onThrowableError(e)
                }
            }
        }
        errorMethod(e)
        endRequestMethod()
    }
}

/**
 * 请求开始
 */
fun <T> NetworkObserver<T>.startRequest(startRequestMethod: (d: Disposable) -> Unit): NetworkObserver<T> {
    this.startRequestMethod = startRequestMethod
    return this
}

/**
 * 成功
 */
fun <T> NetworkObserver<T>.success(successMethod: (value: T?) -> Unit): NetworkObserver<T> {
    this.successMethod = successMethod
    return this
}

/**
 * 失败
 */
fun <T> NetworkObserver<T>.error(isUnifiedHand: Boolean? = true, errorMethod: (e: Throwable) -> Unit): NetworkObserver<T> {
    this.isUnifiedHand = isUnifiedHand
    this.errorMethod = errorMethod
    return this
}

/**
 * 请求结束
 */
fun <T> NetworkObserver<T>.endRequest(endRequestMethod: () -> Unit): NetworkObserver<T> {
    this.endRequestMethod = endRequestMethod
    return this
}
